package com.example.liyuehui.paging.datasource;

import android.arch.lifecycle.MutableLiveData;
import android.arch.paging.PageKeyedDataSource;
import android.support.annotation.NonNull;
import android.util.Log;

import com.example.liyuehui.paging.http.HttpMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Response;

public class TextDataSource extends PageKeyedDataSource<Integer, String> {

	public MutableLiveData<TextDataSourceFactory.NetStatus> netStatusLiveData;
	int currentPage = 1;

	public TextDataSource(MutableLiveData<TextDataSourceFactory.NetStatus> netStatusLiveData) {
		this.netStatusLiveData = netStatusLiveData;
	}


	@Override
	public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, String> callback) {
		netStatusLiveData.postValue(TextDataSourceFactory.NetStatus.INIT);
		List<String> list = new ArrayList<>();
		Call<Object> list1 = HttpMethod.getService().getList(currentPage, params.requestedLoadSize);
		if (list1 != null) {
			try {
				Response<Object> execute = list1.execute();
				Map<String, Object> map = (Map<String, Object>) execute.body();
				List<Map<String, String>> response = (List<Map<String, String>>) map.get("response");
//				Object response1 = response.get("response");
				for (int i = 0; i < response.size(); i++) {
					list.add((String) response.get(i).get("goods_name"));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Log.e("-", "params.requestedLoadSize:" + params.requestedLoadSize + " Thread:" + Thread.currentThread().getName());

		netStatusLiveData.postValue(TextDataSourceFactory.NetStatus.LOAD_END);
		currentPage++;
		callback.onResult(list, null, currentPage);
	}

	@Override
	public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {

	}

	@Override
	public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {
		currentPage++;
		netStatusLiveData.postValue(TextDataSourceFactory.NetStatus.LOADING);
		Log.e("-", "loadAfter:" + params.key + " Thread:" + Thread.currentThread().getName());
		List<String> list = new ArrayList<>();
		Call<Object> list1 = HttpMethod.getService().getList(currentPage, params.requestedLoadSize);
		if (list1 != null) {
			try {
				Response<Object> execute = list1.execute();
				Map<String, Object> map = (Map<String, Object>) execute.body();
				List<Map<String, String>> response = (List<Map<String, String>>) map.get("response");
//				Object response1 = response.get("response");
				for (int i = 0; i < response.size(); i++) {
					list.add((String) response.get(i).get("goods_name"));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (currentPage == 5) {
			netStatusLiveData.postValue(TextDataSourceFactory.NetStatus.BOTTOM);
			callback.onResult(list, null);
		} else {
			netStatusLiveData.postValue(TextDataSourceFactory.NetStatus.LOADING);
			callback.onResult(list, currentPage);
		}

	}
}
